home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / postscripthandledemo / pshdldemo.p < prev   
Encoding:
Text File  |  2000-09-28  |  5.0 KB  |  229 lines

  1. {
  2.     File:        PSHdlDemo.p
  3.  
  4.     Contains:    PSHdlDemo demonstrates how to use PostScriptHandle to send PostScript
  5.                  from a text file to the printer.
  6.  
  7.     Written by: Dave Hersey    
  8.  
  9.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/26/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. }
  24.  
  25. PROGRAM PSHdlDemo;
  26.  
  27. USES
  28.     Memory, QuickDraw, ToolUtils, Traps, Printing, Packages, PrintComments,StandardFile,Fonts,Events;
  29.  
  30.  
  31. {*------ SendPostScript ------------------------------------------------------------*}
  32.  
  33. PROCEDURE SendPostScript(theBuffer: Ptr; buffSize : Longint);
  34. VAR
  35.     dataHdl        : Handle;
  36.     err            : OSErr;
  37.     
  38. BEGIN
  39.     
  40.     err := PtrToHand(theBuffer, dataHdl, buffSize);
  41.  
  42.     IF (err = noErr) THEN
  43.     BEGIN
  44.         PicComment(PostScriptHandle, buffSize, dataHdl);
  45.         DisposeHandle(dataHdl);
  46.     END;
  47. END;
  48.  
  49.  
  50. {*------ DrawStuff -----------------------------------------------------------------*}
  51.  
  52. {**
  53.  **      DrawStuff will open up a text document and send it to the printer
  54.  **      as PostScript.  Hopefully, the text WILL BE PostScript!
  55.  **}
  56.  
  57.  PROCEDURE DrawStuff(theFile: SFReply);
  58.  
  59.  CONST
  60.     fourK    = 4 * 1024;
  61.  
  62.  VAR
  63.        bytesToRead    , amtToRead    : Longint;
  64.     theBuffer                : Ptr;
  65.     err                        : OSErr;
  66.     refNum                    : Integer;
  67.    
  68.        postStr                    : Str255;
  69.    
  70.  BEGIN
  71.  
  72.     { Set up a clipping region.  Unless we do some QuickDraw, no clipping region will
  73.       be set up and everything will be clipped, giving us a blank page. }
  74.  
  75.     PenSize(0, 0);
  76.     MoveTo(0, 0); 
  77.     Line(0, 0);
  78.     PenSize(1, 1);
  79.     
  80.     { Set up the normal PostScript translation/rotation factors.  the Printing Manager
  81.       changes these to match QuickDraw, and if we're just using straight PostScript,
  82.       that will mess us up.}
  83.  
  84.     postStr := '0 728 translate 1 -1 scale ';
  85.     SendPostScript(@postStr[1], Longint(postStr[0]));
  86.  
  87.  
  88.     { Open our file, and determine its length. }
  89.  
  90.     err := FSOpen(theFile.fName, theFile.vRefNum, refNum);
  91.  
  92.     IF (err = noErr) THEN
  93.         err := GetEOF(refNum, bytesToRead);
  94.  
  95.     
  96.     { Read in its contents and send it to the printer using PostScriptHandle.
  97.       We read the data in in 4K blocks, because the driver will break it
  98.       down to that size anyway. }
  99.  
  100.  
  101.     theBuffer := NewPtr(fourK);
  102.     
  103.     WHILE ((err = noErr) AND (bytesToRead <> 0)) DO
  104.     BEGIN
  105.         
  106.         IF bytesToRead > fourK THEN
  107.             amtToRead := fourK
  108.         ELSE
  109.         BEGIN
  110.             amtToRead := bytesToRead;
  111.             SetPtrSize(theBuffer, amtToRead);
  112.         END;
  113.         
  114.         err := FSRead(refNum, amtToRead, theBuffer);
  115.         
  116.         IF (err = noErr) THEN
  117.         BEGIN
  118.             bytesToRead := bytestoRead - amtToRead;
  119.             SendPostScript(theBuffer, amtToRead);
  120.         END;
  121.     
  122.     END;
  123.     
  124.     err := FSClose(refNum);
  125.     
  126.  END;  {**  DrawStuff  **}
  127.  
  128.  
  129.  
  130.  
  131. {*------ PrintStuff ----------------------------------------------------------------*}
  132. {**
  133.  **        PrintStuff gets a text file to print (as PostScript) from the user and then
  134.  **        makes all the print calls to print it.
  135.  **}
  136.  
  137. PROCEDURE PrintStuff;
  138.  
  139.     VAR
  140.         numCopies, copy        : Integer;
  141.         oldPort              : GrafPtr;
  142.         thePrRecHdl            : THPrint;
  143.         thePrPort            : TPPrPort;
  144.         theStatus            : TPrStatus;
  145.         theFile                : SFReply;
  146.         textTypes            : SFTypeList;
  147.         where                : Point;
  148.     
  149. BEGIN
  150.  
  151.     GetPort(oldPort);
  152.     
  153.     SetPt(where, 100, 100);
  154.     textTypes[0] := 'TEXT';
  155.     SFGetFile(where, '', nil, 1, @textTypes, nil, theFile);
  156.     
  157.     thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  158.     
  159.     IF (theFile.good) AND (MemError = noErr) AND (thePrRecHdl <> nil) THEN
  160.     BEGIN
  161.         PrOpen;
  162.  
  163.         IF (PrError = noErr) THEN
  164.         BEGIN
  165.             PrintDefault(thePrRecHdl);
  166.  
  167.             IF (PrError = noErr) THEN
  168.             BEGIN
  169.                IF PrStlDialog(thePrRecHdl) THEN
  170.                 BEGIN
  171.                     IF PrJobDialog(thePrRecHdl) THEN 
  172.                     BEGIN
  173.                         
  174.                         numCopies := thePrRecHdl^^.prJob.iCopies;
  175.  
  176.                         FOR copy := 1 TO numCopies DO
  177.                         BEGIN
  178.                         
  179.                             thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
  180.                         
  181.                             IF (PrError = noErr) THEN
  182.                             BEGIN
  183.     
  184.                                 PrOpenPage(thePrPort, nil);
  185.                                     
  186.                                 IF (PrError = noErr) THEN
  187.                                     DrawStuff(theFile);
  188.                                      
  189.                                 PrClosePage(thePrPort);
  190.                                 
  191.                             END;
  192.                                  
  193.                             PrCloseDoc(thePrPort);
  194.  
  195.                         END;
  196.                         
  197.                         IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  198.                             PrPicFile(thePrRecHdl, nil, nil, nil, @theStatus);
  199.  
  200.                     END;
  201.                 END;
  202.             END;
  203.         END;
  204.         
  205.         PrClose;
  206.  
  207.     END;
  208.      
  209.     IF (thePrRecHdl <> nil) THEN DisposeHandle(Handle(thePrRecHdl));
  210.  
  211. END;  {**  PrintStuff  **}
  212.  
  213.  
  214. {*------ main ----------------------------------------------------------------------*}
  215.  
  216. BEGIN
  217.     
  218.     InitGraf(@qd.thePort);
  219.     InitFonts;
  220.     FlushEvents(everyEvent, 0);    
  221.     InitWindows;
  222.     InitMenus;
  223.     TEInit;
  224.     InitDialogs(nil);
  225.     InitCursor;
  226.  
  227.     PrintStuff;
  228.  
  229. END. {**  main  **}